layui.use(['layer', 'form', 'ax', 'table', 'admin', 'laydate'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    var SysDictGroupModule = {
        tableId: "SysDictGroupModuleTable"
    };
    var tableResult = table.render({
        elem: '#' + SysDictGroupModule.tableId,
        url: Feng.ctxPath + '/sysDict/getDictGroupPage',
        page: true,
        size: 'sm',
        height: "full-140",
        cellMinWidth: 100,
        even: true,
        // toolbar: '#toolbarDemo',
        request: { pageName: 'current', limitName: 'size'},
        // response: { statusName: 'code' ,statusCode: 200 ,msgName: 'message' ,countName: 'total' ,dataName: 'records' },
        parseData: function(res){
            return {
                "code": 0, //解析接口状态
                // "msg": res.message, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.records //解析数据列表
            };
        },
        cols: [[
            {field: 'groupId',   title: '主键' ,hide: true ,align: 'center'},
            {field: 'groupName', title: '字典组名称' ,align: 'center'},
            {field: 'groupCode', title: '字典组编号' ,align: 'center'},
            {field: 'createTime',title: '创建时间' ,align: 'center' ,hide: true},
            {toolbar: '#tableBarSysDictGroup', title: '操作',align: 'center'}
        ]]
    });
    // 工具条点击事件
    table.on('tool(' + SysDictGroupModule.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'btnEdit') {
            admin.putTempData('formOk', false);
            top.layui.admin.open({
                type: 2,
                title: '修改',
                content: Feng.ctxPath + '/sysDict/sysDictGroupEditPage?groupId=' + data.groupId,
                end: function () {
                    admin.getTempData('formOk') && table.reload(SysDictGroupModule.tableId);
                }
            });
        } else if (layEvent === 'btnDel') {
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/sysDict/sysDictGroupRemove/"+data.groupId, function (res) {
                    Feng.success("删除成功!");
                    table.reload(SysDictGroupModule.tableId);
                }, function (res) {
                    Feng.error("删除失败!" + res.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("是否删除?", operation);
        }
    });
    // 重置
    $('#btnAdd').click(function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '新增',
            content: Feng.ctxPath + '/sysDict/sysDictGroupAddPage',
            end: function () {
                admin.getTempData('formOk') && table.reload(SysDictGroupModule.tableId);
            }
        });
    });
    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        SysDictGroupModule.search();
    });
    SysDictGroupModule.search = function () {
        var queryData = {};
        queryData['groupName'] = $("#groupName").val();
        table.reload(SysDictGroupModule.tableId, {where: queryData});
    };
    // 重置
    $('#btnReset').click(function () {
        $("#groupName").val("");
        SysDictGroupModule.search();
    });


    ////行单击事件
    table.on('row(SysDictGroupModuleTable)', function(obj){
        // console.log(obj.tr);
        //obj.del(); //删除当前行
        //obj.update(fields) //修改当前行数据
        // console.log(obj.data.groupCode);
        $("#groupCode").val(obj.data.groupCode);
        $("#groupCode").attr("disabled","true");
        SysDictItemModule.search();
    });

///////////////////////////////////FIXME 字典项////////////////////////////
    var SysDictItemModule = {
        tableId: "SysDictItemModuleTable"
    };
    var tableResult2 = table.render({
        elem: '#' + SysDictItemModule.tableId,
        url: Feng.ctxPath + '/sysDict/getDictItemPage',
        page: false,
        size: 'sm',
        height: "full-140",
        limit: 20,
        cellMinWidth: 100,
        even: true,
        // toolbar: '#toolbarDemo',
        request: { pageName: 'current', limitName: 'size'},
        // response: { statusName: 'code' ,statusCode: 200 ,msgName: 'message' ,countName: 'total' ,dataName: 'records' },
        parseData: function(res){
            return {
                "code": 0, //解析接口状态
                // "msg": res.message, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.records //解析数据列表
            };
        },
        cols: [[
            {field: 'itemId',    title: '主键' ,hide: true ,align: 'center'},
            {field: 'groupCode', title: '字典组编号' ,align: 'center',width: 150},
            {field: 'itemCode',  title: '字典项编号' ,align: 'center',edit: 'text'},
            {field: 'itemValue', title: '字典项名称' ,align: 'center',edit: 'text'},
            {toolbar: '#tableBarSysDictItem', title: '操作',align: 'center'}
        ]]
    });

    // 工具条点击事件
    table.on('tool(' + SysDictItemModule.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'btnEditSysDictItem') {
            admin.putTempData('formOk', false);
            top.layui.admin.open({
                type: 2,
                title: '修改',
                content: Feng.ctxPath + '/sysDict/sysDictItemEditPage?itemId=' + data.itemId,
                end: function () {
                    admin.getTempData('formOk') && table.reload(SysDictItemModule.tableId);
                }
            });
        } else if (layEvent === 'btnDelSysDictItem') {
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/sysDict/sysDictItemRemove/"+data.itemId, function (res) {
                    Feng.success("删除成功!");
                    table.reload(SysDictItemModule.tableId);
                }, function (res) {
                    Feng.error("删除失败!" + res.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("是否删除?", operation);
        }
    });

    //监听单元格编辑
    table.on('edit(SysDictItemModuleTable)', function(obj){
        var value = obj.value; //得到修改后的值
        var data = obj.data; //得到所在行所有键值
        var field = obj.field; //得到字段
        var ajax = new $ax(Feng.ctxPath + "/sysDict/sysDictItemUpdate", function (data) {
            Feng.success("修改成功！");
        }, function (data) {
            Feng.error("修改失败！" + data.responseJSON.message)
        });
        ajax.set(data);
        ajax.start();
    });


    // 搜索按钮点击事件
    $('#btnSearchSysDictItem').click(function () {
        SysDictItemModule.search();
    });
    SysDictItemModule.search = function () {
        var queryData = {};
        queryData['groupCode'] = $("#groupCode").val();
        table.reload(SysDictItemModule.tableId, {where: queryData});
    };
    /// 重置
    $('#btnResetSysDictItem').click(function () {
        $("#groupCode").val("");
        SysDictItemModule.search();
    });
    //// 添加
    $('#btnAddSysDictItem').click(function () {
        if($("#groupCode").val()==""){
            return false;
        }
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '新增',
            content: Feng.ctxPath + '/sysDict/sysDictItemAddPage?groupCode='+$("#groupCode").val(),
            end: function () {
                admin.getTempData('formOk') && table.reload(SysDictItemModule.tableId);
            }
        });
    });



});
